package org.hash.leetCode0017;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wjy
 * @Date: 2021/12/6 10:13
 */
public class Solution {

    public static void main(String[] args) {

        Solution solution = new Solution();
        List<String> stringList = solution.letterCombinations("23");
        for (String str : stringList) {
            System.out.println(str);
        }

    }

    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if (digits.length() == 0) {
            return res;
        }

        Map<Character, String> map = new HashMap<>();
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");

        this.recursion(res, map, new StringBuilder(), digits, 0);
        return res;
    }

    public void recursion(List<String> res, Map<Character, String> map, StringBuilder sb, String digits, int index) {
        if (index == digits.length()) {
            res.add(sb.toString());
            return;
        }
        char c = digits.charAt(index);
        String s = map.get(c);
        for (int i = 0; i < s.length(); i++) {
            sb.append(s.charAt(i));
            this.recursion(res, map, sb, digits, index + 1);
            sb.deleteCharAt(index);
        }
    }

}
